package com.fx.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * des:
 *
 * @author fxiao
 * @date 2020/12/10 16:49
 */
@Entity(name = "t_user")
public class User implements UserDetails , Serializable {

    public User(String username, String password, Role ... roles) {
        this.username = username;
        this.password = password;
        this.roles = new ArrayList<>(Arrays.asList(roles));
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(){

    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String username;
    private String password;
    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.REFRESH)
    @JoinTable(
            name="userRole"
            ,joinColumns = {@JoinColumn(name="userId")}
            ,inverseJoinColumns = {@JoinColumn(name = "roleId")}
    )

    private List<Role> roles;
    /**账户未过期？*/
    private boolean accountNonExpired=true;
    /**账户未锁定？*/
    private boolean accountNonLocked=true;
    /**密码未过期*/
    private boolean credentialsNonExpired=true;
    /**用户未禁用？*/
    private boolean enabled=true;

    public Long getId() {
        return id;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * 返回用户的泛权限集合。<br>
     * 所谓的泛权限就是指包括了前面加“ROLE_”前缀的角色<br>
     * 不允许返回null。
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority>authorities=new ArrayList<>();
        for (Role role:roles){
            //此处的权限是泛权限，将角色前面添加“ROLE_”前缀之后也要返回。
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getCode()));
            for (Resource resou:role.getResources()) {
                authorities.add(new SimpleGrantedAuthority(resou.getCode()));
            }
        }
        return authorities;
    }

    /**
     * 返回用户密码
     * @return
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * 返回用户名
     * @return
     */
    @Override
    public String getUsername() {
        return username;
    }

    /**
     * 账户未过期？
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    /**
     * 账户是否为锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    /**
     * 密码是否未过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    /**
     * 账户是否可用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
